package com.cn.customer.housekeeping.housekeepingb.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.amap.api.services.core.AMapException;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.Inputtips.InputtipsListener;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.cn.customer.housekeeping.housekeepingb.R;
import com.cn.customer.housekeeping.housekeepingb.bean.BaseResp;
import com.cn.customer.housekeeping.housekeepingb.biz.personcenter.IUserLoginView;
import com.cn.customer.housekeeping.housekeepingb.bridge.BridgeFactory;
import com.cn.customer.housekeeping.housekeepingb.bridge.Bridges;
import com.cn.customer.housekeeping.housekeepingb.bridge.cache.sharePref.EBSharedPrefManager;
import com.cn.customer.housekeeping.housekeepingb.bridge.cache.sharePref.EBSharedPrefUser;
import com.cn.customer.housekeeping.housekeepingb.ui.base.BaseActivity;
import com.cn.customer.housekeeping.housekeepingb.ui.base.QuickAdapter;
import com.cn.customer.housekeeping.housekeepingb.ui.helper.ItemHelper;
import com.cn.customer.housekeeping.housekeepingb.util.NavUtils;
import com.cn.customer.housekeeping.housekeepingb.util.ToastUtil;
import com.lfq.pulltorefresh.library.PullToRefreshBase;
import com.lfq.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/***
 * 选择位置
 */
public class ChoicePositionActivity extends BaseActivity implements IUserLoginView, TextWatcher, InputtipsListener {
    private ListView mListView;
    private QuickAdapter<HashMap<String, String>> mAdapter;
    private PullToRefreshListView mPullToRefreshListView;
    private AutoCompleteTextView mKeywordText;
    private EBSharedPrefManager manager = BridgeFactory.getBridge(Bridges.SHARED_PREFERENCE);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_choiceposition);
        super.onCreate(savedInstanceState);
    }
    RelativeLayout relativeLayout8;
    @Override
    public void initViews() {
        mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        relativeLayout8 = (RelativeLayout) findViewById(R.id.relativeLayout8);
        mKeywordText = (AutoCompleteTextView) findViewById(R.id.input_edittext);
    }

    @Override
    public void initListeners() {
        mKeywordText.addTextChangedListener(ChoicePositionActivity.this);
//        mKeywordText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                if (hasFocus) {
//                    //got focus
//                    relativeLayout8.setVisibility(View.VISIBLE);
//                } else {
//                    //lost focus
//                    relativeLayout8.setVisibility(View.GONE);
//                }
//            }
//        });
    }

    @Override
    public void initData() {
        ItemHelper ch = new ItemHelper();
        mPullToRefreshListView.setMode(PullToRefreshBase.Mode.DISABLED);
        mListView = mPullToRefreshListView.getRefreshableView();
        mListView.setHeaderDividersEnabled(false);
        mListView.setDividerHeight(0);
        mAdapter = ch.setAdapterChoicePosition(this, new ArrayList<HashMap<String, String>>());
        mListView.setAdapter(mAdapter);
        mPullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });

        mPullToRefreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {

            }
        });
        mPullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int headerCount = mListView.getHeaderViewsCount();
                if (position >= headerCount) {
                    HashMap<String, String>  item = mAdapter.getItem(position - headerCount);
//                    Intent intent = new Intent();
//                    intent.putExtra("name",  item.get("name") );
//                    intent.putExtra("address",  item.get("address") );
//                    intent.putExtra("Lat",  item.get("Lat") );
//                    intent.putExtra("Long",  item.get("Long") );
//                    setResult(RESULT_OK, intent);
//                    finish();

                    NavUtils.gotoPlaceDetailsActivity(ChoicePositionActivity.this, 1003, item.get("name") ,item.get("address"),item.get("Lat"), item.get("Long") );

                }
            }
        });

        onSearch(manager.getKDPreferenceUserInfo().getString(EBSharedPrefUser.getAoiName, ""));


    }

    @Override
    public void setHeader() {
        super.setHeader();
        rlTitle.setVisibility(View.VISIBLE);
        ivTopBack.setVisibility(View.VISIBLE);
        tvTopTitle.setText("所在位置");
    }


    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String newText = s.toString().trim();
        onSearch(newText);
    }

    public void onSearch(String newText) {
        InputtipsQuery inputquery = new InputtipsQuery(newText, "" + manager.getKDPreferenceUserInfo().getString(EBSharedPrefUser.citycode, ""));
        inputquery.setCityLimit(true);
        Inputtips inputTips = new Inputtips(ChoicePositionActivity.this, inputquery);
        inputTips.setInputtipsListener(this);
        inputTips.requestInputtipsAsyn();
    }


    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

    /**
     * 输入提示结果的回调
     *
     * @param tipList
     * @param rCode
     */
    @Override
    public void onGetInputtips(final List<Tip> tipList, int rCode) {
        if (rCode == AMapException.CODE_AMAP_SUCCESS) {
            List<HashMap<String, String>> listString = new ArrayList<HashMap<String, String>>();
            for (int i = 0; i < tipList.size(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("name", tipList.get(i).getName());
                map.put("address", tipList.get(i).getDistrict()+" "+ tipList.get(i).getAddress());
                map.put("Lat", tipList.get(i).getPoint().getLatitude()+"");
                map.put("Long", tipList.get(i).getPoint().getLongitude()+"");
                listString.add(map);
            }
            mAdapter.replaceAll(listString);

        } else {
            ToastUtil.makeText(ChoicePositionActivity.this, rCode + "");
        }

    }

    @Override
    public void onError(BaseResp s) {

    }

    @Override
    public void onSuccess(BaseResp s) {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {

            case 1003:
                if (data != null) {
                    Intent intent = new Intent();
                    intent.putExtra("name",(String) data.getExtras().get("name"));
                    intent.putExtra("address", (String) data.getExtras().get("address") );
                    intent.putExtra("Lat",(String) data.getExtras().get("Lat") );
                    intent.putExtra("Long", (String) data.getExtras().get("Long") );
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;


        }
    }
}
